package com.example.sociamedia.post.controller;

import com.example.sociamedia.post.dto.LikeRequest;
import com.example.sociamedia.post.dto.LikeResponse;
import com.example.sociamedia.post.entity.Like;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/likes")
public class LikeController {
    private final LikeService likeService;

    @Autowired
    public LikeController(LikeService likeService) {
        this.likeService = likeService;
    }

    @PostMapping("")
    public ResponseEntity<LikeResponse> like(@RequestHeader("Authorization") String sessionToken, @RequestBody LikeRequest request) {
        LikeResponse resp = likeService.likePost(sessionToken, request);
        return new ResponseEntity<>(resp, resp.isSuccess() ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
    }

    @DeleteMapping("")
    public ResponseEntity<LikeResponse> unlike(@RequestHeader("Authorization") String sessionToken, @RequestBody LikeRequest request) {
        LikeResponse resp = likeService.unlikePost(sessionToken, request);
        return new ResponseEntity<>(resp, resp.isSuccess() ? HttpStatus.OK : HttpStatus.BAD_REQUEST);
    }

    @GetMapping("/count/{postId}")
    public ResponseEntity<Integer> getLikeCount(@PathVariable Long postId) {
        return ResponseEntity.ok(likeService.getLikeCount(postId));
    }

    @GetMapping("/users/{postId}")
    public ResponseEntity<List<Like>> getLikesByPost(@PathVariable Long postId) {
        return ResponseEntity.ok(likeService.getLikesByPost(postId));
    }

    @GetMapping("/posts/{userId}")
    public ResponseEntity<List<Like>> getLikesByUser(@PathVariable Long userId) {
        return ResponseEntity.ok(likeService.getLikesByUser(userId));
    }
} 